<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app {
            width: 300px;
        }

        ul {
            width: 300px;
            list-style: none;
        }

        ul li {
            width: 150px;
            float: left;
            text-align: center;
        }

        ul li img {
            width: 150px;
            height: 150px;
        }
    </style>
</head>

<body>
    <div id="app">
        <h2>{{ text }}</h2>
        <goods-cpn :text="text" @gettext="getText"></goods-cpn>
    </div>
    <template id="goodsCpn">
        <ul>
            <li v-for="(item,index) in goodsList" :key="item.id" @click="getText(index)">
                <img :src="item.img" alt="">
                <p>{{ item.type }}</p>
                <p>{{ item.price }}</p>
            </li>
        </ul>
    </template>
    <script src="./js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data() {
                return {
                    text: '手机1'
                }
            },
            methods: {
                getText(val) {
                    this.text = val;
                }
            },
            components: {
                'goods-cpn': {
                    template: '#goodsCpn',
                    data() {
                        return {
                            goodsList: [
                                {
                                    id: 1,
                                    img: './img/1.png',
                                    type: '手机1',
                                    price: '￥5999'
                                },
                                {
                                    id: 2,
                                    img: './img/2.png',
                                    type: '手机2',
                                    price: '￥5999'
                                },
                                {
                                    id: 3,
                                    img: './img/3.jpg',
                                    type: '手机3',
                                    price: '￥5999'
                                },
                                {
                                    id: 4,
                                    img: './img/4.png',
                                    type: '手机4',
                                    price: '￥5999'
                                },
                                {
                                    id: 5,
                                    img: './img/5.png',
                                    type: '手机5',
                                    price: '￥5999'
                                },
                                {
                                    id: 6,
                                    img: './img/6.png',
                                    type: '手机6',
                                    price: '￥5999'
                                },
                                {
                                    id: 7,
                                    img: './img/7.jpg',
                                    type: '手机7',
                                    price: '￥5999'
                                },
                                {
                                    id: 8,
                                    img: './img/8.png',
                                    type: '手机8',
                                    price: '￥5999'
                                }
                            ]
                        }
                    },
                    props: ['text'],
                    methods: {
                        getText(i) {
                            let newText = this.text;
                            newText = this.goodsList[i].type;
                            console.log(newText);
                            this.$emit('gettext', newText);
                        }
                    }

                }
            }
        })
    </script>
</body>

</html>